local zhuobi = fk.CreateSkill {
  name = "th_zhuobi",
}

zhuobi:addEffect(fk.TargetSpecifying, {
  anim_type = "control",
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(zhuobi.name) and data.card and data.firstTarget then
      if not (data.card.type == Card.TypeBasic or data.card:isCommonTrick()) then return false end
      if data.card.trueName == "collateral" then return false end --stupid collateral
      local origin_targets = table.filter(player.room.alive_players, function(e) return table.contains(data.use.tos, e) end)
      if not (data.from == player or table.contains(origin_targets, player)) then return false end
      local targets = data:getExtraTargets({ bypass_distances = true, bypass_times = true })
      table.insertTableIfNeed(targets, origin_targets)
      if #targets > 0 then
        event:setCostData(self, targets)
        return true
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local ask_data = {
      targets = table.map(event:getCostData(self), Util.IdMapper),
      num = 3,
      min_num = 1,
      skillName = zhuobi.name,
      extra_data = table.map(data:getAllTargets(), Util.IdMapper),
    }
    local _, ret = room:askToUseActiveSkill(player, {
      skill_name = "#th_zhuobi_active",
      prompt = "#th_zhuobi-choose:::"..data.card:toLogString(),
      cancelable = true,
      extra_data = ask_data
    })
    local to = {}
    if ret then
      to = ret.targets
    end
    if #to > 0 then
      event:setCostData(self, { tos = to })
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local tos = event:getCostData(self).tos
    for _, to in ipairs(tos) do
      if table.contains(data.use.tos, to) then
        data:cancelTarget(to)
      else
        data:addTarget(to)
      end
    end
    local num = #tos
    local cards = player:getCardIds("h")
    local dif = #cards - num
    if dif < 0 then
      player:drawCards(- dif, zhuobi.name)
      cards = player:getCardIds("h")
    elseif dif > 0 then
      cards = room:askToCards(player, {
        min_num = num,
        max_num = num,
        include_equip = false,
        skill_name = zhuobi.name,
        cancelable = false,
        prompt = "#th_zhuobi-show:::" .. num
      })
    end
    player:showCards(cards)
    for _, pid in ipairs(cards) do
      room:addCardMark(Fk:getCardById(pid, true), "@@th_zhuobi-inhand")
    end
  end,
})
zhuobi:addEffect("invalidity", {
  name = "#th_zhuobi_invaild",
  invalidity_func = function(self, from, skill)
    if from and skill.name == "th_zhuobi" then
      for _, cid in ipairs(from:getCardIds("h")) do
        local card = Fk:getCardById(cid, true)
        if card:getMark("@@th_zhuobi-inhand") ~= 0 then
          return true
        end
      end
    end
  end
})

Fk:loadTranslationTable{
  ["th_zhuobi"] = "琢壁",
  [":th_zhuobi"] = "不为【借刀杀人】的基本牌或普通锦囊牌指定目标时，若你是使用者或目标之一，你可以为此牌增加或减少至多三个目标（无距离限制），然后展示等量张手牌（不足则先摸至等量张），此技能失效直到这些牌离开你的手牌区。",

  ["#th_zhuobi-choose"] = "琢壁：你可以为 %arg 增加或减少至多三个目标（无距离限制）",
  ["#th_zhuobi-show"] = "琢壁：请展示 %arg 张手牌，此技能将无效直到这些牌离开你的手牌区",

  ["@@th_zhuobi-inhand"] = "琢壁",
}

return zhuobi